package com.rw.tool.util.io.file.wrapper;

import com.rw.tool.util.string.StringUtil;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询条件
 */
@Getter
public class FileQueryWrapper {
    // 查询条件列表
    private final List<QueryCondition> conditions;

    public FileQueryWrapper() {
        this.conditions = new ArrayList<>();
    }

    /**
     * 加入自定义条件
     *
     * @param condition 条件
     */
    public FileQueryWrapper addCondition(QueryCondition condition) {
        conditions.add(condition);
        return this;
    }

    /**
     * 等于
     */
    public FileQueryWrapper eq(String property, Object value) {
        conditions.add(new QueryCondition(property, CompareSign.EQ, value));
        return this;
    }


    /**
     * 大于
     */
    public FileQueryWrapper gt(String property, Object value) {
        conditions.add(new QueryCondition(property, CompareSign.GT, value));
        return this;
    }


    /**
     * 大于等于
     */
    public FileQueryWrapper ge(String property, Object value) {
        conditions.add(new QueryCondition(property, CompareSign.GE, value));
        return this;
    }

    /**
     * 小于
     */
    public FileQueryWrapper lt(String property, Object value) {
        conditions.add(new QueryCondition(property, CompareSign.LT, value));
        return this;
    }

    /**
     * 小于等于
     */
    public FileQueryWrapper le(String property, Object value) {
        conditions.add(new QueryCondition(property, CompareSign.LE, value));
        return this;
    }

    /**
     * 模糊查询
     */
    public FileQueryWrapper like(String property, Object value) {
        conditions.add(new QueryCondition(property, CompareSign.LIKE, value));
        return this;
    }

    /**
     * 进行匹配
     *
     * @param data 数据
     * @return 是否匹配通过
     */
    public boolean match(Object data) {
        if (data == null) {
            return false;
        }

        return conditions.stream().allMatch(condition -> condition.match(data));
    }

    /**
     * 获得条件
     *
     * @param name 字段名字
     * @return 条件
     */
    public QueryCondition getCondition(String name) {
        return conditions.stream()
                .filter(condition -> StringUtil.equals(name, condition.getProperty()))
                .findAny()
                .orElse(null);
    }

    /**
     * 获得等于条件
     *
     * @param name 字段名字
     * @return 条件
     */
    public Object getEQValue(String name) {
        return conditions.stream()
                .filter(condition -> StringUtil.equals(name, condition.getProperty()))
                .filter(condition -> CompareSign.EQ.equals(condition.getCompareSign()))
                .map(QueryCondition::getValue)
                .findAny()
                .orElse(null);
    }
}
